<?php


namespace app\engine\controller;

use cmf\controller\HomeBaseController;
use think\Db;

class SubcontController extends HomeBaseController
{

    public function index()
    {
        $data = $this->request->param();
        $id = $data['id'];
        $res = Db::name('Subcont')->where(['eid' => $id])->select()->toArray();
        $this->assign('res', $res);
        $this->assign('id', $id);
        return $this->fetch();
    }

    public function add()
    {
        $role = Db::name('role_user')->where(['user_id'=>cmf_get_current_admin_id()])->value('role_id');
        if($role == 2 || $role == 4 || $role == 5){
            $this->error('无权限操作');
        }
        $data = $this->request->param();
        $id = $data['id'];
        $this->assign('id', $id);
        return $this->fetch();
    }

    public function doadd()
    {
        $data = $this->request->param();
        if (empty($data['company']) || empty($data['position']) || empty($data['name']) || empty($data['mobile'])) {
            $this->error('缺少必要参数');
        }
//        $check = Db::name('subcont')->where(['eid'=>$data['eid'],'company'=>$data['company']])->find();
        $checkName = Db::name('subcont')->where(['eid' => $data['eid'], 'name' => $data['name']])->find();
        $checkMobile = Db::name('subcont')->where(['eid' => $data['eid'], 'mobile' => $data['mobile']])->find();
//        if($check){
//            $this->error('该分包单位名称');
//        }
        if ($checkName) {
            $this->error('该姓名已经存在');
        }
        if ($checkMobile) {
            $this->error('该手机号已存在');
        }
        $iner = [
            'company' => $data['company'],
            'position' => $data['position'],
            'name' => $data['name'],
            'mobile' => $data['mobile'],
            'eid' => $data['eid']
        ];
        $res = Db::name('subcont')->insert($iner);
        if (!$res) {
            $this->error('添加失败');
        }
        $this->success('添加成功');
    }

    public function edit()
    {
        $data = $this->request->param();
        $res = Db::name('subcont')->where(['id' => $data['id']])->find();
        $this->assign('res', $res);
        $this->assign('id', $res['eid']);
        return $this->fetch();
    }

    public function doedit()
    {
        $role = Db::name('role_user')->where(['user_id'=>cmf_get_current_admin_id()])->value('role_id');
        if($role == 2 || $role == 4 || $role == 5){
            $this->error('无权限操作');
        }
        $data = $this->request->param();
        if (empty($data['company']) || empty($data['position']) || empty($data['name']) || empty($data['mobile'])) {
            $this->error('缺少必要参数');
        }
//        $check = Db::name('subcont')->where(['eid'=>$data['eid'],'company'=>$data['company']])->where('id','<>',$data['id'])->find();
        $checkName = Db::name('subcont')->where(['eid' => $data['eid'], 'name' => $data['name']])->where('id', '<>', $data['id'])->find();
        $checkMobile = Db::name('subcont')->where(['eid' => $data['eid'], 'mobile' => $data['mobile']])->where('id', '<>', $data['id'])->find();
//        if($check){
//            $this->error('该分包单位名称');
//        }
        if ($checkName) {
            $this->error('该姓名已经存在');
        }
        if ($checkMobile) {
            $this->error('该手机号已存在');
        }
        $uper = [
            'company' => $data['company'],
            'position' => $data['position'],
            'name' => $data['name'],
            'mobile' => $data['mobile']
        ];
        $res = Db::name('subcont')->where(['id' => $data['id']])->update($uper);
        if (!$res) {
            $this->error('操作失败');
        }
        $this->success('操作成功');

    }

    public function del()
    {
        $role = Db::name('role_user')->where(['user_id'=>cmf_get_current_admin_id()])->value('role_id');
        if($role == 2 || $role == 4 || $role == 5){
            $this->error('无权限操作');
        }
        $data = $this->request->param();
        $res = Db::name('subcont')->where(['id' => $data['id']])->delete();
        if (!$res) {
            $this->error('操作失败');
        }
        $this->success('操作成功');
    }

}